package com.zhz.elasticjob.zookeeper.config;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: schedule
 * @description: zookeeper的配置类
 * @author: zhanghz001
 * @create: 2021-08-05 09:39
 **/
@Configuration
@EnableConfigurationProperties(ZookeeperProperties.class)
//满足一定条件才注入
@ConditionalOnProperty(prefix = "elasticjob.zookeeper", name = "serverLists")
public class ElasticJobZookeeperConfig {
    @Autowired
    private ZookeeperProperties zookeeperProperties;
    
    /**
     * 配置了zookeeper的信息,
     * 将bean交给spring管理
     *
     * @return
     */
    @Bean
    public CoordinatorRegistryCenter createRegistryCenter() {
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(
                new ZookeeperConfiguration(
                        zookeeperProperties.getServerLists(),
                        zookeeperProperties.getNamespace())
        );
        regCenter.init();
        return regCenter;
    }
}
